<?php
/**
 * Author:          Andrei Baicus <andrei@themeisle.com>
 * Created on:      2019-02-11
 *
 * @package Neve Pro Addon
 */

namespace Neve_Pro\Modules\Woocommerce_Booster\Views;

use Neve\Views\Base_View;

/**
 * Class Single_Product_Video
 *
 * @package Neve_Pro\Modules\Woocommerce_Booster\Views
 */
class Single_Product_Video extends Base_View {


	/**
	 * Initialize the module.
	 */
	public function init() {
		add_filter(
			'woocommerce_single_product_image_thumbnail_html',
			array(
				$this,
				'replace_featured_image',
			),
			10,
			2
		);

		add_filter( 'woocommerce_placeholder_img_src', array( $this, 'add_video_thumbnail_to_placeholder' ) );
		add_filter( 'woocommerce_single_product_image_gallery_classes', array( $this, 'add_gallery_classes' ) );
	}

	/**
	 * Register submodule hooks
	 */
	public function register_hooks() {
		$this->init();
	}

	/**
	 * Add video thumbnail as placeholder when we have a video but not a product image.
	 *
	 * @param string $src Thumbnail URL.
	 *
	 * @return string
	 */
	public function add_video_thumbnail_to_placeholder( $src ) {
		if ( ! is_product() ) {
			return $src;
		}
		$video_url = $this->get_video_url();
		if ( empty( $video_url ) ) {
			return $src;
		}

		return $this->get_video_thumbnail( $video_url );
	}

	/**
	 * Add classes to the gallery.
	 *
	 * @param array $classes classes for the gallery.
	 *
	 * @return array
	 */
	public function add_gallery_classes( $classes ) {
		if ( ! is_product() ) {
			return $classes;
		}
		$vid_url = $this->get_video_url();
		if ( empty( $vid_url ) ) {
			return $classes;
		}
		$classes[] = 'nv-video-product';
		if ( strpos( $vid_url, 'vimeo' ) || strpos( $vid_url, 'youtube' ) || strpos( $vid_url, 'youtu.be' ) ) {
			return $classes;
		}
		$classes[] = 'nv-video-file-src';

		return $classes;
	}

	/**
	 * Replaces featured image with video
	 *
	 * @param int $html         The html.
	 * @param int $thumbnail_id The thumbnail id.
	 *
	 * @return string
	 */
	public function replace_featured_image( $html, $thumbnail_id ) {

		if ( ! is_product() ) {
			return $html;
		}
		$video_url = $this->get_video_url();

		if ( empty( $video_url ) ) {
			return $html;
		}
		$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );

		if ( $thumbnail_id !== $post_thumbnail_id ) {
			return $html;
		}

		if ( empty( $thumbnail_id ) ) {
			$html = '';
		}

		$video = '<div data-thumb="' . $this->get_video_gallery_thumbnail() . '" class="woocommerce-product-gallery__image nv-featured-video">';

		$video .= '<div class="nv-video-wrap" id="nv-featured-vid">';
		$video .= $this->get_iframe( $video_url );
		$video .= '</div>';

		$video .= '</div>';

		$html = $video . $html;

		return $html;
	}

	/**
	 * Get the video iframe.
	 *
	 * @param string $url the video url.
	 *
	 * @return string
	 */
	private function get_iframe( $url ) {
		if ( empty( $url ) ) {
			return '';
		}

		if ( strpos( $url, 'youtube' ) || strpos( $url, 'youtu.be' ) ) {
			$parts = parse_url( $url );
			parse_str( $parts['query'], $query );
			$video_id = $query['v'];

			return '<iframe src="https://www.youtube.com/embed/' . esc_attr( $video_id ) . '?enablejsapi=1"  
			allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen" frameborder="0"></iframe>';
		}

		if ( strpos( $url, 'vimeo' ) ) {
			$id = substr( $url, strrpos( $url, '/' ) + 1 );

			return '<iframe src="https://player.vimeo.com/video/' . esc_attr( $id ) . '?loop=1&title=0&byline=0&portrait=0" frameborder="0"></iframe>';
		}

		return '<video height="350" controls><source src="' . esc_url( $url ) . '" type="video/mp4"></video>';
	}

	/**
	 * Gets the standard gallery image.
	 *
	 * @return string
	 */
	private function get_video_gallery_thumbnail() {
		$b64 = '';

		return apply_filters( 'neve_pro_product_video_gallery_thumb_src', $b64 );
	}

	/**
	 * Get thumbnail for the video url.
	 *
	 * @param string $url the video url.
	 *
	 * @return string
	 */
	private function get_video_thumbnail( $url ) {
		if ( strpos( $url, 'youtube' ) || strpos( $url, 'youtu.be' ) ) {
			return $this->get_youtube_thumbnail( $url );
		}
		if ( strpos( $url, 'vimeo' ) ) {
			return $this->get_vimeo_thumbnail( $url );
		}
	}

	/**
	 * Get YouTube thumbnail.
	 *
	 * @param string $url the video url.
	 *
	 * @return string
	 */
	private function get_youtube_thumbnail( $url ) {
		$parts = parse_url( $url );
		parse_str( $parts['query'], $query );
		$video_id = $query['v'];

		return esc_url( 'https://img.youtube.com/vi/' . $video_id . '/sddefault.jpg' );
	}

	/**
	 * Get vimeo thumbnail.
	 *
	 * @param string $url the video url.
	 *
	 * @return string
	 */
	private function get_vimeo_thumbnail( $url ) {
		$id  = substr( $url, strrpos( $url, '/' ) + 1 );
		$api = unserialize( file_get_contents( "https://vimeo.com/api/v2/video/$id.php" ) );

		return esc_url( $api[0]['thumbnail_small'] );
	}

	/**
	 * Get the video URL from the meta field.
	 *
	 * @return string
	 */
	private function get_video_url() {
		return get_post_meta( get_the_ID(), 'neve_meta_product_video_link', true );
	}
}
